package state.component;

import state.GameConstants;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Manager;
import state.Sprite;
import state.util.array.StringArray;

public class GameMenu extends MenuItem {
  public EventListener listener = null;
  public MenuItem currentMenuItem = null;
  public GameImage itemBackgroundImage = null;
  public boolean fillFocusItemBg = false;

  public int itemWidth = 80;
  public int itemHeight = 30;
  public boolean openChildMenuToRight = true;

  public int menuX = 0;
  public int menuY = 0;
  public int textDx = 0;
  public int textDy = 0;

  public BottomBar bottomBar = null;

  public GameMenu(Manager manager1, String[] menuItems, int len, EventListener listener1, MenuStyle style, int x, int y) {
    super("");
    if (style != null) {
      setStyleForMenu(style);
    }
    if (menuItems != null) {
      addItems(new StringArray(menuItems, len));
    }

    if (listener1 != null) {
      setListener(listener1);
    }

    if (manager1 != null) {
      setManager(manager1);
    }
    menuX = x;
    menuY = y;
  }

  public void initComponent() {
    super.initComponent();
    menu = this;
    parent = this;
    currentMenuItem = this;
    setWidth(GameConstants.SCREEN_WIDTH);
    setHeight(GameConstants.SCREEN_HEIGHT);
    _realX = 0;
    _realY = 0;
    setVisible(true);
  }

  public Sprite setPosition(int x, int y) {
    menuX = x;
    menuY = y;
    return this;
  }

  public BottomBar getBottomBar() {
    return bottomBar;
  }

  public void setBottomBar(BottomBar bottomBar1) {
    this.bottomBar = bottomBar1;
  }

  public void setItemSize(int itemWidthm, int itemHeight1) {
    setItemWidth(itemWidthm);
    setItemHeight(itemHeight1);
  }

  public Component setStyleForMenu(MenuStyle style) {
    super.setStyle(style);
    itemWidth = style.itemWidth;
    itemHeight = style.itemHeight;
    openChildMenuToRight = style.openChildMenuToRight;
    textDx = style.textDx;
    textDy = style.textDy;
    fillFocusItemBg = style.fillFocusItemBg;
    itemBackgroundImage = style.itemBackgroundImage;
    return this;
  }

  public void setMenuItemBgImage(GameImage image) {
    setImage1(image, false);
  }

  public boolean isFillFocusItemBg() {
    return fillFocusItemBg;
  }

  public void setFillFocusItemBg(boolean isFillFocusItemBg) {
    this.fillFocusItemBg = isFillFocusItemBg;
  }

  public void setMenuX(int menuX1) {
    this.menuX = menuX1;
  }

  public void setMenuY(int menuY1) {
    this.menuY = menuY1;
  }

  public int getMenuX() {
    return menuX;
  }

  public int getMenuY() {
    if (itemBackgroundImage == null) {
      return menuY;
    }
    return menuY;
  }

  public int getTextDx() {
    return textDx;
  }

  public void setTextDx(int textDx1) {
    this.textDx = textDx1;
  }

  public int getTextDy() {
    return textDy;
  }

  public void setTextDy(int textDy1) {
    this.textDy = textDy1;
  }

  public void show() {
    showChildMenu();
  }

  public boolean isShowing() {
    return isShowChildrenMenuItem;
  }

  public void hide() {
    hideChildMenu();
  }

  public boolean isOpenChildMenuToRight() {
    return openChildMenuToRight;
  }

  public void setOpenChildMenuToRight(boolean isOpenChildMenuToRight) {
    this.openChildMenuToRight = isOpenChildMenuToRight;
  }

  public void setListener(EventListener listener1) {
    this.listener = listener1;
  }

  public int getItemWidth() {
    return itemWidth;
  }

  public void setItemWidth(int itemWidth1) {
    this.itemWidth = itemWidth1;
    for (int i = 0; i < items.size(); i++) {
      ((Component) items.elementAt(i)).setWidth(itemWidth1);
    }
  }

  public GameImage getItemBackgroundImage() {
    return itemBackgroundImage;
  }

  public void setItemBackgroundImage(GameImage itemBackgroundImage1) {
    this.itemBackgroundImage = itemBackgroundImage1;
  }

  public int getItemHeight() {
    return itemHeight;
  }

  public void setItemHeight(int itemHeight1) {
    this.itemHeight = itemHeight1;
    for (int i = 0; i < items.size(); i++) {
      ((Component) items.elementAt(i)).setHeight(itemHeight1);
    }
  }

  public void draw(Graphics g) {
    if (!isVisible()) {
      return;
    }

    if (isShowChildrenMenuItem && (items.size() > 0)) {
      isShowUpToDown = checkDrawUpOrDown();
      for (int i = 0; i < items.size(); i++) {
        MenuItem item = (MenuItem) items.elementAt(i);
        updateChildPosition(item, i, isShowUpToDown);
        item.draw(g);
      }
    }
  }

  public void keyPressed(Sprite source, int keyCode) {
    if (!isShowing()) {
      return;
    }

    if (bottomBar != null) {
      Manager manager1 = bottomBar.getManager();
      if (manager1 != null) {
        manager1.stopEvent();
      }
    }
  }

  public void keyRepeated(Sprite source, int keyCode) {
    if (!isShowing()) {
      return;
    }

    if (bottomBar != null) {
      Manager manager1 = bottomBar.getManager();
      if (manager1 != null) {
        manager1.stopEvent();
      }
    }
  }

  public void keyReleased(Sprite source, int keyCode) {
    if (!isShowing()) {
      return;
    }

    if (bottomBar != null) {
      Manager manager1 = bottomBar.getManager();
      if (manager1 != null) {
        manager1.stopEvent();
      }
    }

    MenuItem currentParent = menu.currentMenuItem.parent;
    if (currentParent.currentChooseIndex > currentParent.items.size() - 1) {
      currentChooseIndex = currentParent.items.size() - 1;
    }

    switch (keyCode) {
    case Key.FIRE:
      if (menu.currentMenuItem.items.size() > 0) {
        menu.currentMenuItem.showChildMenu();
      } else {
        if (menu.listener != null) {
          menu.listener.onActionPerform(new Event(menu.currentMenuItem, menu.currentMenuItem.label1));
          hide();
        }
      }
      break;
    case Key.RIGHT:
      if (menu.openChildMenuToRight) {
        menu.currentMenuItem.showChildMenu();
      } else {
        if (currentParent != this) {
          currentParent.hideChildMenu();
        }
      }
      break;
    case Key.LEFT:
      if (!menu.openChildMenuToRight) {
        menu.currentMenuItem.showChildMenu();
      } else {
        if (currentParent != this) {
          currentParent.hideChildMenu();
        }
      }
      break;
    case Key.UP:
      if (currentParent.items.size() > 0) {
        menu.currentMenuItem.setFocused(false);
        currentParent.currentChooseIndex--;
        if (currentParent.currentChooseIndex < 0) {
          currentParent.currentChooseIndex = currentParent.items.size() - 1;
        }
        menu.currentMenuItem = ((MenuItem) currentParent.items.elementAt(currentParent.currentChooseIndex));
        menu.currentMenuItem.setFocused(true);
      }
      break;
    case Key.DOWN:
      if (currentParent.items.size() > 0) {
        menu.currentMenuItem.setFocused(false);
        currentParent.currentChooseIndex++;
        if (currentParent.currentChooseIndex > currentParent.items.size() - 1) {
          currentParent.currentChooseIndex = 0;
        }
        menu.currentMenuItem = ((MenuItem) currentParent.items.elementAt(currentParent.currentChooseIndex));
        menu.currentMenuItem.setFocused(true);
      }
      break;
    }
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    if (items.size() > 0 && isShowing()) {
      for (int i = 0; i < items.size(); i++) {
        MenuItem item = (MenuItem) items.elementAt(i);
        item.pointerReleased(item, x1, y1);
      }
    }
  }

  public void updateChildPosition(MenuItem item, int itemIndex, boolean isShowUpToDown1) {
    int x1 = menuX;
    int y1 = getMenuY();
    if (isShowUpToDown1) {
      y1 = y1 + itemIndex * (menu.itemHeight - 1) + textDy;
    } else {
      y1 = y1 - (items.size() - itemIndex - 1) * (menu.itemHeight - 1) - textDy;
    }
    item.setPosition(x1, y1);
  }

  public Sprite detroy() {
    super.detroy();
    listener = null;
    currentMenuItem = null;
    itemBackgroundImage = null;
    return this;
  }
}
